package com.yykt.myapp.controller;

import com.yykt.myapp.entity.User;
import com.yykt.myapp.service.UserRepository;
import com.yykt.myapp.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("jpa")
@RestController
public class JpaSample {

    @Autowired
    UserRepository userRepository;

    @GetMapping("findAll")
    public JsonResult findAll() {
        List<User> all = userRepository.findAll();
        return JsonResult.success(all);
    }

    @GetMapping("findById")
    public JsonResult findById(@RequestParam Long id) {
        User user = userRepository.findById(id).get();
        return JsonResult.success(user);
    }

    @GetMapping("findByAk")
    public JsonResult findByAk(@RequestParam String ak) {
        List<User> user = userRepository.findByAk(ak);
        return JsonResult.success(user);
    }
}
